import { createStore } from 'vuex'

export default createStore({
  state: {
    todos: []
  },
  mutations: {
    ADD_TODO(state, todo) {
      state.todos.push(todo)
    },
    TOGGLE_TODO(state, index) {
      state.todos[index].completed = !state.todos[index].completed
    },
    DELETE_TODO(state, index) {
      state.todos.splice(index, 1)
    }
  },
  actions: {
    addTodo({ commit }, todo) {
      commit('ADD_TODO', todo)
    },
    toggleTodo({ commit }, index) {
      commit('TOGGLE_TODO', index)
    },
    deleteTodo({ commit }, index) {
      commit('DELETE_TODO', index)
    }
  },
  getters: {
    allTodos: (state) => state.todos
  }
})
